﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SDLL;

namespace SABI
{
    public partial class frm0070 : Form
    {
        bool _isNew = true;
        string sql = "";
        string _DivisionID;

        public frm0070()
        {
            InitializeComponent();
        }

        public frm0070(bool isNew=true, bool isView=false, string DivisionID=null)
        {
            InitializeComponent();
            btnSave.Visible = !isView;
            txtDivisionID.Enabled = isNew;
            btnAdd.Visible = isNew;
            _isNew = isNew;
            _DivisionID = DivisionID;
        }

        private void frm0070_Load(object sender, EventArgs e)
        {

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckInput()) { return; }
            if(!_isNew)
            {
                UpdateInfo();
            }
            else
            {
                AddNew();
            }
        }

        private bool CheckInput()
        {
            if(txtDivisionID.Text.Trim()=="")
            {
                DLL02.Alert("Bạn phải nhập Mã đơn vị!");
                txtDivisionID.Focus();
                return false;
            }
            else if(txtDivisionName.Text.Trim()=="")
            {
                DLL02.Alert("Bạn phải nhập Tên đơn vị!");
                txtDivisionName.Focus();
                return false;
            }
            return true;
        }

        private void AddNew()
        {
            sql = "INSERT tb0038(DivisionID,DivisionName,Disabled,Tel,Fax,";
            sql += "Email,Address,ContactPerson,VATNo,BeginMonth,BeginYear,";
            sql += "CreateDate,CreateUserID,LastModifyDate,LastModifyUserID,";
            sql += "DivisionNameE,AddressE,DirectorName,ChiefAccountantName,";
            sql += "StoreKeeperName,CashierName,ReportCreatorName1,ReportCreatorName2,";
            sql += "Info1,Info2,Info3,Info4,Info5,";
            sql += "Representative, Website,BankAccountNo) VALUES(";
            sql += string.Format("'{0}',N'{1}','{2}','{3}','{4}',", txtDivisionID.Text, txtDivisionName.Text, chkDisabled.Checked?1:0, txtTel.Text, txtFax.Text);
            sql += string.Format("'{0}',N'{1}',N'{2}','{3}','{4}','{5}',", txtEmail.Text, txtAddress.Text, txtContactPerson.Text, txtVATNo.Text, txtBeginMonth.Value, txtBeginYear.Value);
            sql += string.Format("{0},'{1}',{2},'{3}',", "GETDATE()", FRM.UserName, "GETDATE()", FRM.UserName);
            sql += string.Format("'{0}','{1}',N'{2}',N'{3}',", txtDivisionNameE.Text, txtAddressE.Text, txtDirectorName.Text, txtChiefAccountantName.Text);
            sql += string.Format("N'{0}',N'{1}',N'{2}',N'{3}',", txtStoreKeeperName.Text, txtCashierName.Text, txtReportCreatorName1.Text, txtReportCreatorName2.Text);
            sql += string.Format("N'{0}',N'{1}',N'{2}',N'{3}',N'{4}',", txtInfo1.Text, txtInfo2.Text, txtInfo3.Text, txtInfo4.Text, txtInfo5.Text);
            sql += string.Format("N'{0}',N'{1}','{2}')", txtRepresentative.Text, txtWebsite.Text, txtBankAccountNo.Text);
            if(SDATA.DATA.ExecuteNonQuery(sql,FRM.ConnectString,false))
            {
                SDLL.DLL02.Info("Lưu dữ liệu thành công!");
                (Application.OpenForms["frm0061"] as frm0061).loadDATA();
                btnSave.Enabled = false;
                txtDivisionID.Enabled = false;
            }
            else
            {
                SDLL.DLL02.Alert("Lưu dữ liệu thất bại!");
            }            
        }

        private void UpdateInfo()
        {
            sql = string.Format("UPDATE tb0038 SET DivisionName=N'{0}', Disabled='{1}', Tel='{2}', Fax='{3}',", txtDivisionName.Text,chkDisabled.Checked?1:0,txtTel.Text,txtFax.Text);
            sql += string.Format("Email=N'{0}',Address=N'{1}',ContactPerson=N'{2}',VATNo='{3}',BeginMonth='{4}',BeginYear='{5}',", txtEmail.Text, txtAddress.Text, txtContactPerson.Text, txtVATNo.Text, txtBeginMonth.Value, txtBeginYear.Value);
            sql += string.Format("LastModifyDate=GETDATE(),LastModifyUserID='{0}',", FRM.UserName);
            sql += string.Format("DivisionNameE='{0}',AddressE='{1}',DirectorName=N'{2}',ChiefAccountantName=N'{3}',", txtDivisionNameE.Text,txtAddressE.Text,txtDirectorName.Text,txtChiefAccountantName.Text);
            sql += string.Format("StoreKeeperName=N'{0}',CashierName=N'{1}',ReportCreatorName1=N'{2}',ReportCreatorName2=N'{3}',",txtStoreKeeperName.Text,txtCashierName.Text,txtReportCreatorName1.Text,txtReportCreatorName2.Text);
            sql += string.Format("Info1=N'{0}',Info2=N'{1}',Info3=N'{2}',Info4=N'{3}',Info5=N'{4}',",txtInfo1.Text,txtInfo2.Text,txtInfo3.Text,txtInfo4.Text,txtInfo5.Text);
            sql += string.Format("Representative=N'{0}', Website=N'{1}',BankAccountNo='{2}' WHERE DivisionID='{3}'", txtRepresentative.Text, txtWebsite.Text, txtBankAccountNo.Text, txtDivisionID.Text);
            if (SDATA.DATA.ExecuteNonQuery(sql, FRM.ConnectString, false))
            {
                SDLL.DLL02.Info("Cập nhật dữ liệu thành công!");
                (Application.OpenForms["frm0061"] as frm0061).loadDATA();
                Close();
            }
            else
            {
                SDLL.DLL02.Alert("Cập nhật dữ liệu thất bại!");
            }
        }


        private void frm0070_Shown(object sender, EventArgs e)
        {
            loadDATA_Combo();
            if (!_isNew)
            {
                sql = string.Format("SELECT * FROM tb0038 WHERE DivisionID='{0}'", _DivisionID);
                DataTable dt = FRM.GetDataTable(sql);
                if (dt.Rows.Count < 1) { return; }
                txtDivisionID.Text = dt.Rows[0]["DivisionID"].ToString();
                txtDivisionName.Text = dt.Rows[0]["DivisionName"].ToString();
                txtDivisionNameE.Text = dt.Rows[0]["DivisionNameE"].ToString();
                txtTel.Text = dt.Rows[0]["Tel"].ToString();
                txtEmail.Text = dt.Rows[0]["Email"].ToString();
                txtWebsite.Text = dt.Rows[0]["Website"].ToString();
                txtFax.Text = dt.Rows[0]["Fax"].ToString();
                txtAddress.Text = dt.Rows[0]["Address"].ToString();
                txtAddressE.Text = dt.Rows[0]["AddressE"].ToString();
                txtContactPerson.Text = dt.Rows[0]["ContactPerson"].ToString();
                txtRepresentative.Text = dt.Rows[0]["Representative"].ToString();
                txtVATNo.Text = dt.Rows[0]["VATNo"].ToString();
                txtBankAccountNo.Text = dt.Rows[0]["BankAccountNo"].ToString();
                txtDirectorName.Text = dt.Rows[0]["DirectorName"].ToString();
                txtStoreKeeperName.Text = dt.Rows[0]["StoreKeeperName"].ToString();
                txtChiefAccountantName.Text = dt.Rows[0]["ChiefAccountantName"].ToString();
                txtCashierName.Text = dt.Rows[0]["CashierName"].ToString();
                txtReportCreatorName1.Text = dt.Rows[0]["ReportCreatorName1"].ToString();
                txtReportCreatorName2.Text = dt.Rows[0]["ReportCreatorName2"].ToString();
                txtInfo1.Text = dt.Rows[0]["Info1"].ToString();
                txtInfo2.Text = dt.Rows[0]["Info2"].ToString();
                txtInfo3.Text = dt.Rows[0]["Info3"].ToString();
                txtInfo4.Text = dt.Rows[0]["Info4"].ToString();
                txtInfo5.Text = dt.Rows[0]["Info5"].ToString();
                txtBeginMonth.Value = (int)dt.Rows[0]["BeginMonth"];
                txtBeginYear.Value = (int)dt.Rows[0]["BeginYear"];
                //TDBCboCurrencyID.Text = dt.Rows[0]["CurrencyID"].ToString();
                //txtPeriodNum.Text = dt.Rows[0]["PeriodNum"].ToString();
                //TDBCboBranchID.Text = dt.Rows[0]["BranchID"].ToString();
                chkDisabled.Checked = dt.Rows[0]["Disabled"].ToString() == "1" ? true : false;
            }            
        }

        private void loadDATA_Combo()
        {
            // Loại tiền
            sql = "Select * From tb0024 Where Disabled =0 Order by CurrencyID";
            TDBCboCurrencyID.DataSource = FRM.dt_GET(ref FRM.dt_Currency, sql);
            TDBCboCurrencyID.Text = "VND";

            // Chi nhánh
            sql = "Select BranchID, BranchName From tb0042 Where Disabled = 0  Order by BranchID";
            TDBCboBranchID.DataSource = FRM.GetDataTable(sql);

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            btnSave.Enabled = true;
            txtDivisionID.Enabled = true;
            _isNew = true;
            foreach(Control c in this.Controls)
                if(c.GetType()== typeof(TextBox))
                    c.Text = "";
        }

    }
}
